var Common = require('../../utils/common.js');
import create from '../../utils/create2.js'
import store from '../../store/index.js'
const { $Toast } = require('../../component/base/index');
create(store, {

  /**
   * 页面的初始数据
   */
  data: {
    tasting: {},
    coupon: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { coupon } = options
    this.setData({
      coupon
    })
      Common.my_request({
        url: '/AppMaster/DinnerRoom/getDinnerCoupon',
        data: {
          dinner_coupon_code: coupon
        },
        success: (res) => {
          const { data, result, message } = res.data
          if (result === 0) {
            this.store.data.tasting = data
            this.store.data.tasting['dinner_coupon_code'] = coupon
            this.update()
          } else {
            //优惠券不存在或者错误时 跳首页
            Common.Alert('提示', message, () => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            });
          }
        }
      });
  },
  createHandler() {
    const userid = wx.getStorageSync('user_id');
    if (isNaN(userid) || userid <= 0) {
      const { coupon} = this.data
      Common.Alert('提示', '请先登录', () => {
        wx.navigateTo({
          url: `/pages/my/logintype/logintype?tasting=${coupon}&from=tasting`,
        })
      })
      return
    } 
    const { tasting } = this.data
    const { salesman_id } = tasting
    if (salesman_id) {
      Common.my_request({
        url: '/AppMaster/Account/selectNewConsultant',
        data: {
          salesman_id
        },
        success: (res) => {
          if (res.data.result === 0) {
            Common.when_user_change_clear_the_storage();
          }
        }
      });
    }
    wx.navigateTo({
      url: '/pages/index/foodform/index?from=pjh',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})